/**
 * LeaveNoteController
 *
 * @description :: Server-side logic for managing Leavenotes
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */

module.exports = {
		
	listAjax : function(req, res){
		//"skip":0,"limit":30,"sort":"updatedAt DESC"
		var skip = req.param("skip");
		var limit = req.param("limit");
		var sort = req.param("sort");
		
		LeaveNote.find()
			.sort('updatedAt DESC')
			.populate('form')
			.populate('type')
			.limit(limit)
			.sort(sort)
			.skip(skip)
			.then(function (notes) {
				return res.send(notes);
			}).catch(function(err){
				sails.log.error(err);
				return res.serverError(err);
			});
		
	},
	
	getAjax : function(req, res){
		var pkid = req.param("id");
		LeaveNote.findOne()
			.populate('form')
			.populate('type')
			.then(function (note) {
				return res.send(note);
			}).catch(function(err){
				sails.log.error(err);
				return res.serverError(err);
			});
	},
	
	list : function(req, res){
		var owner = req.session.user.userid;
		var month = req.param("current");
		var action = req.param("action");
		var dateMonth = FormService.getMonthByAction(month, action);
		
		
		LeaveNote.find({ 'createdAt': { '>=': dateMonth.current.toDate(), '<': dateMonth.next.toDate() } })
			.sort('updatedAt DESC')
			.populate('form')
			.populate('type')
			.then(function (notes) {
				
				//Filter mine
				var myNotes = [];
				for (var i = 0; i < notes.length; i++) {
					if(notes[i].form.owner == owner){
						myNotes.push(notes[i]);
					}
				}
				
				return res.view('mobile/forms/note-list', {
					notes : myNotes,
					current : dateMonth.current.format("YYYYMM")
				});
			}).catch(function(err){
				sails.log.error(err);
				return res.serverError(err);
			});
		
		
		
	},
	
	get : function(req, res){
		
		var method = req.param("method");
		var resview = 'mobile/forms/note-create';
		
		if(method == 'update' || method == 'detail'){
			var pkid = req.param("pkid");
			
			//Leave Note
			LeaveNote.findOne({'id' : pkid})
			.populate('type')
			.then(function (note){
				
				//Form
				Form.findOne({'id' : note.form})
				.populate('owner')
				.then(function (form){
					
					//FormOpinion
					FormOpinion.find({'form':form.id})
					.sort('updatedAt DESC')
					.populate('superior')
					.then(function (opinions){
						
						if(method == 'detail'){
							resview = 'mobile/forms/note-detail';
							
						}else if(method == 'update'){
							resview = 'mobile/forms/note-update';
						}
						
						form.opinions = opinions;
						note.form = form;
						
						return res.view(resview, {
							//leaveTypes : types,
							note : note
						});
						
					})
				});
			});
		}else {
			//LeaveType
			LeaveType.find()
			.sort('seq ASC')
			.then(function (types) {
				return res.view(resview, {
					leaveTypes : types,
				});
			}).catch(function(err){
				sails.log.error(err);
				return res.serverError(err);
			});
			
		}
		
		
	},
	
	send : function(req, res){

		var pkid = req.body.pkid;
		var userid = req.body.user;
		//var url = req.body.url;
		var url = sails.config.application.appPath;
		
		LeaveNote
		.findOne({'id':pkid})
		.then(function (note) {
			Form
			.update({'id':note.form}, { 'waitChecker':userid, 'lastChecker':null, 'state':2})
			.then(function (forms) {
				if(forms.length > 0){
					WechatService.sendFormToChecker(forms[0], url, req, function(err, result){
						if(result.errcode == 0){
							return res.send({success : true});
						}else{
							return res.send({
								success : false,
								error : err
							});
						}
					});
				}
			});
		}).catch(function(err){
			sails.log.error(err);
			return res.send({
				success : false,
				error : err
			});
		});
	},
		
};

